home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 363 / xprolog2 / _boot next >
Encoding:
Text File  |  1985-11-19  |  5.7 KB  |  221 lines

  1. /*
  2.  
  3.  *        X PROLOG  Vers. 2.0
  4.  
  5.  *
  6.  
  7.  *
  8.  
  9.  *    Written by :     Andreas Toenne
  10.  
  11.  *            CS Dept. , IRB
  12.  
  13.  *            University of Dortmund, W-Germany
  14.  
  15.  *            <atoenne@unido.uucp>
  16.  
  17.  *            <....!seismo!unido!atoenne>
  18.  
  19.  *            <atoenne@unido.bitnet>
  20.  
  21.  *
  22.  
  23.  *    Copyright :    This software is copyrighted by Andreas Toenne.
  24.  
  25.  *            Permission is granted hereby to copy the entire
  26.  
  27.  *            package including this copyright notice without fee.
  28.  
  29.  *
  30.  
  31.  */
  32.  
  33.  
  34.  
  35. %    X Prolog Boot File
  36.  
  37.  
  38.  
  39. % hack to create an intermediate goal for call
  40.  
  41. % this make the cut local to call
  42.  
  43.  
  44.  
  45. call(A) :- $call(A).
  46.  
  47.  
  48.  
  49. % definitions for conjunction and disjunction
  50.  
  51. % both procedures are made transparent to the cut
  52.  
  53.  
  54.  
  55. (A ; B) :- $call(A).
  56.  
  57. (A ; B) :- $call(B).
  58.  
  59.  
  60.  
  61. (A , B) :- $call(A), $call(B).
  62.  
  63.  
  64.  
  65. % further predicates
  66.  
  67.  
  68.  
  69. not(Predicate) :- call(Predicate), !, fail.
  70.  
  71. not(Predicate).
  72.  
  73.  
  74.  
  75. clause(Head, Body) :- $clause(Head, Body, Help).  % see the documentation
  76.  
  77.  
  78.  
  79. A = A.                    % equality predicate :-)
  80.  
  81.  
  82.  
  83. print(Term) :- var(Term), !, write(Term).
  84.  
  85. print(Term) :- portray(Term).        % portray should be user defined
  86.  
  87.  
  88.  
  89. append([],L,L).                % common append procedure
  90.  
  91. append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
  92.  
  93.  
  94.  
  95. member(X, [X|_]).            % common member procedure
  96.  
  97. member(X, [_|Y]) :- member(X, Y).
  98.  
  99.  
  100.  
  101. % toplevel interpreter loop
  102.  
  103. % the main goal should not be changed
  104.  
  105.  
  106.  
  107. main :- $loop(toplevel).        % start things
  108.  
  109. main.                    % just to make Xprolog happy
  110.  
  111.  
  112.  
  113. % this is a failure driven loop
  114.  
  115.  
  116.  
  117. $loop(toplevel) :-
  118.  
  119.     prompt(Old, '|    '),        % change the default prompt
  120.  
  121.     repeat,                % loop forever
  122.  
  123.         $prompt('?- '),        % give a prompt
  124.  
  125.         read(Term),        % wait for response
  126.  
  127.         $solve(Term, toplevel),    % solve the query
  128.  
  129.     prompt(_, Old),            % restore the prompt
  130.  
  131.     !.
  132.  
  133. $loop(Where) :-                % loop not at top level
  134.  
  135.     prompt(Old, '| '),        % different default prompt
  136.  
  137.     repeat,                % round and round again
  138.  
  139.         prompt_if_user,        % no prompt for files
  140.  
  141.         read(Term),        % read something
  142.  
  143.         $solve(Term, Where),    % solve it
  144.  
  145.     prompt(_, Old),            % restore the prompt
  146.  
  147.     !.
  148.  
  149.     
  150.  
  151. prompt_if_user :- seeing(user), $prompt('| '), !.
  152.  
  153. prompt_if_user.
  154.  
  155.  
  156.  
  157. $solve(end_of_file, _) :- !.        % the only way to stop the repeat
  158.  
  159. $solve(Term, _) :- var(Term), !, fail.    % don't accept strange goals
  160.  
  161. $solve(Term, Where) :-            % try to solve it as a goal
  162.  
  163.     $query(Term, Where, Goal, What), % check for sort of question
  164.  
  165.     !,
  166.  
  167.     prompt(Old, '|: '),
  168.  
  169.     $solve_goal(Goal, What),    % try to solve a goal
  170.  
  171.     prompt(_, Old),
  172.  
  173.     fail.
  174.  
  175. $solve(Term, Where) :-            % try to assert it
  176.  
  177.     $process(Term, Result),        % hook for preprocessors
  178.  
  179.     assertz(Result),        % assert it
  180.  
  181.     !,
  182.  
  183.     fail.
  184.  
  185. $solve(Term, _) :-            % assert or $process failed
  186.  
  187.     write('! clause: '),
  188.  
  189.     write(Term),
  190.  
  191.     fail.
  192.  
  193.     
  194.  
  195. % this is a hook to add preprocessors like the grammar rule translator
  196.  
  197. % to this top level interpreter.
  198.  
  199. % simply add via 'asserta' another clause for the preprocessor
  200.  
  201.  
  202.  
  203. $process(T,T).
  204.  
  205.  
  206.  
  207. % check the current term for a question or a command
  208.  
  209.  
  210.  
  211. $query(:-(X), _, X, command) :- !.    % this is a command
  212.  
  213. $query(?-(X), _, X, question) :- !.    % this is a question
  214.  
  215. $query(X, toplevel, X, question).    % always questions on top level
  216.  
  217.  
  218.  
  219. % this procedure solves goals
  220.  
  221. % note the use of $more and $goalvars
  222.  
  223.  
  224.  
  225. $solve_goal(Term, command) :-        % no answer, no alternatives
  226.  
  227.     call(Term),            % try it once
  228.  
  229.     !.                % and no further alternatives
  230.  
  231. $solve_goal(_, command)    :-        % above clause failed
  232.  
  233.     !,
  234.  
  235.     nl, write('?'), nl.        % notify the user
  236.  
  237. $solve_goal(Term, question) :-
  238.  
  239.     $goalvars(List),        % save the reader's symbol table
  240.  
  241.     call(Term),            % try the question
  242.  
  243.     $more(Ok),            % call(Term) had a alternative ?
  244.  
  245.     $reply(List, Ok),        % say 'yes' to the user
  246.  
  247.     nl,
  248.  
  249.     !.
  250.  
  251. $solve_goal(_, question) :-        % above clause failed !
  252.  
  253.     nl,
  254.  
  255.     write(no),            % sorry but ...
  256.  
  257.     nl,
  258.  
  259.     !.
  260.  
  261.     
  262.  
  263. $reply(List, Ok) :-            % say yes and show variables
  264.  
  265.     $show_variables(List),
  266.  
  267.     write(yes),            % horray
  268.  
  269.     Ok = yes,            % an alternative ?
  270.  
  271.     $askformore,            % check if the user wants it
  272.  
  273.     !.
  274.  
  275. $reply(_, Ok) :-            % no more alternative
  276.  
  277.     Ok = no,
  278.  
  279.     !.
  280.  
  281.     
  282.  
  283. $askformore :- get(X), skip(10), X \== 59. % 59 is ';'
  284.  
  285.     
  286.  
  287. $show_variables([]) :- !.
  288.  
  289. $show_variables([(Name, Variable)|L]) :-
  290.  
  291.     write(Name),
  292.  
  293.     write(' = '),
  294.  
  295.     write(Variable),
  296.  
  297.     nl,
  298.  
  299.     !,
  300.  
  301.     $show_variables(L).
  302.  
  303.     
  304.  
  305.  
  306.  
  307.  
  308.  
  309. % consult and friends
  310.  
  311. % we simply use the top level interpreter for the asserts and queries
  312.  
  313.  
  314.  
  315. [X|Y] :- $process_files([X|Y]).
  316.  
  317.  
  318.  
  319. $process_files([]) :- !.
  320.  
  321. $process_files([-File|Rest]) :- !, reconsult(File), $process_files(Rest).
  322.  
  323. $process_files([File|Rest]) :- !, consult(File), $process_files(Rest).
  324.  
  325.  
  326.  
  327. consult(File) :- !, $read_file(File, 0).
  328.  
  329.  
  330.  
  331. reconsult(File) :- !, $read_file(File, 1).
  332.  
  333.  
  334.  
  335. $read_file(File, R) :-
  336.  
  337.     Heap is heapused,
  338.  
  339.     Time is cputime,
  340.  
  341.     $reconsulting(R),
  342.  
  343.     $test_filename(File),        % check the file
  344.  
  345.     seeing(OldIn),
  346.  
  347.     telling(OldOut),
  348.  
  349.     see(File),            % open the file
  350.  
  351.     $do_loop,
  352.  
  353.     seen,                % close the file
  354.  
  355.     see(OldIn),
  356.  
  357.     tell(OldOut),
  358.  
  359.     $reconsulting(0),
  360.  
  361.     DiffTime is cputime - Time,
  362.  
  363.     DiffHeap is heapused - Heap,
  364.  
  365.     write(File),
  366.  
  367.     ( R == 0 , write('  consulted ') ;
  368.  
  369.       R == 1 , write('  reconsulted ')),
  370.  
  371.     write(DiffHeap), write(' bytes '),
  372.  
  373.     write(DiffTime), write(' msec.'),
  374.  
  375.     nl, !.
  376.  
  377.  
  378.  
  379. $do_loop :- $loop(filelevel).        % loop at filelevel
  380.  
  381. $do_loop.
  382.  
  383.  
  384.  
  385. $test_filename(user) :- !.        % this stream is always ok
  386.  
  387. $test_filename(File) :-
  388.  
  389.     not atom(File),            % invalid name
  390.  
  391.     nl,
  392.  
  393.     write('Invalid filename: '),
  394.  
  395.     write(File),
  396.  
  397.     nl,
  398.  
  399.     !, fail.
  400.  
  401. $test_filename(File) :-
  402.  
  403.     not exists(File),        % file not found
  404.  
  405.     nl,
  406.  
  407.     write('The file '),
  408.  
  409.     write(File),
  410.  
  411.     write(' does not exist.'),
  412.  
  413.     nl,
  414.  
  415.     !, fail.
  416.  
  417. $test_filename(_).            % is ok
  418.  
  419.  
  420.  
  421. %
  422.  
  423. % debugging hooks
  424.  
  425. %
  426.  
  427.  
  428.  
  429. leash(off) :- $leash(0).
  430.  
  431. leash(loose) :- $leash(1).
  432.  
  433. leash(half) :- $leash(5).
  434.  
  435. leash(tight) :- $leash(7).
  436.  
  437. leash(full) :- $leash(15).
  438.  
  439.  
  440.  
  441.